using System;

namespace Naptime.Extensions
{
	public static class EventExtensions
	{
		public static void ClearDelegates<T>(this Action<T> actionEvent)
		{
			if (actionEvent != null)
			{
				Delegate[] invocationList = actionEvent.GetInvocationList();
				for (int i = 0; i < invocationList.Length; i++)
				{
					Action<T> value = (Action<T>)invocationList[i];
					actionEvent = (Action<T>)Delegate.Remove(actionEvent, value);
				}
			}
		}

		public static void ClearDelegates(this Action actionEvent)
		{
			if (actionEvent != null)
			{
				Delegate[] invocationList = actionEvent.GetInvocationList();
				for (int i = 0; i < invocationList.Length; i++)
				{
					Action value = (Action)invocationList[i];
					actionEvent = (Action)Delegate.Remove(actionEvent, value);
				}
			}
		}

		public static bool HasDelegates(this Action actionEvent)
		{
			if (actionEvent == null)
			{
				return false;
			}
			return actionEvent.GetInvocationList().Length != 0;
		}
	}
}
